"use strict";

const { Controller } = require("egg");

class examController extends Controller {
  async add() {
    const { ctx } = this;
    const req = ctx.request.body;

    const res = await ctx.service.examService.create(
      {
        c_id: req.c_id,
        name: req.name,
        start_time: req.start_time,
        end_time: req.end_time,
        pass_score: req.pass_score,
        total_score:0
      },
      req.u_id
    );
    const json = ctx.helper.json(
      res.data ? res.data : null,
      res.suc ? 200 : 500,
      res.msg
    );
    ctx.body = json;
  }

  async del() {
    const { ctx } = this;
    const req = ctx.request.body;

    const res = await ctx.service.examService.del(req.id, req.u_id);
    ctx.body = ctx.helper.json(
        res.data ? res.data : null,
        res.suc ? 200 : 500,
        res.msg
    );
  }

  async get() {
    const { ctx } = this;
    const req = ctx.request.body;

    const res = await ctx.service.examService.get(req);
    const json = ctx.helper.json(
      res.data ? res.data : null,
      res.suc ? 200 : 500,
      res.msg
    );
    ctx.body = json;
  }

  async getOne() {
    const { ctx } = this;
    const req = ctx.request.body;

    const res = await ctx.service.examService.getOne(req);
    const json = ctx.helper.json(
      res.data ? res.data : null,
      res.suc ? 200 : 500,
      res.msg
    );
    ctx.body = json;
  }

  async getUserAll() {
    const { ctx } = this;
    const req = ctx.request.body;

    const res = await ctx.service.examService.userGetAll(req.u_id);
    const json = ctx.helper.json(
      res.data ? res.data : null,
      res.suc ? 200 : 500,
      res.msg
    );
    ctx.body = json;
  }

  async userManageExam(){
    const { ctx } = this;
    const req = ctx.request.body;

    const res = await ctx.service.examService.userManageExam(req.u_id);
    const json = ctx.helper.json(
      res.data ? res.data : null,
      res.suc ? 200 : 500,
      res.msg
    );
    ctx.body = json;
  }

  async update(){
    const { ctx } = this;
    const req = ctx.request.body;
    const res = await ctx.service.examService.update(req);
    ctx.body = ctx.helper.json(
        res.data ? res.data : null,
        res.suc ? 200 : 500,
        res.msg
    );
  }
}
module.exports = examController;
